﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeshGenerator.Core;

namespace MeshGenerator.Application.Windows.Views
{
    public partial class EditorControl : UserControl
    {
        Mesh model = new Mesh();

        public EditorControl()
        {
            InitializeComponent();
            MouseClick += new MouseEventHandler(EditorControl_MouseClick);
            MouseMove += new MouseEventHandler(EditorControl_MouseMove);
            model.Changed += new EventHandler(model_Changed);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            List<Point> points = new List<Point>(model.VertexData.Count);
            Graphics g = this.CreateGraphics();
            int x,y;
            using (Pen redPen = new Pen(Color.Red))
            {                    
                foreach (var item in model.VertexData)
                {
                    x = (int)item.X;
                    y = (int)item.Y;
                    g.DrawEllipse(redPen, x, y, 2, 2);
                    points.Add(new Point(x, y));
                }
            }

            if (points.Count > 1)
            {
                points.Add(points[0]);
                using (Pen blackPen = new Pen(Color.Black))
                {
                    g.DrawLines(blackPen, points.ToArray());
                }
            }
        }

        void model_Changed(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        void EditorControl_MouseClick(object sender, MouseEventArgs e)
        {
            model.VertexData.Add(new Vertex() { X = e.X, Y = e.Y });
        }

        void EditorControl_MouseMove(object sender, MouseEventArgs e)
        {
            ((MainForm)this.TopLevelControl).MouseLocation = e.Location.ToString(); //string.Format("({0},{1})", e.X, e.Y);
        }
    }
}
